#include "histogram_manager.h"

#include <QCategoryAxis>

HistogramManager::HistogramManager(QObject *parent) : QObject(parent)
{
    chart = new QChart;
}

void HistogramManager::setTitle(const QString &title)
{
    chart->setTitle(title);
    chart->setAnimationOptions(QChart::SeriesAnimations);
}

void HistogramManager::setAxisX(const QStringList &categories, uint16_t gridDensity)
{
    axiList = new QBarCategoryAxis();
    axiList->append(categories);
}

void HistogramManager::setData(QBarSeries *series, int max)
{
    ////创建默认的左侧的坐标轴（根据 QBarSet 设置的值）
//    series->setLabelsFormat(QString("%1:%2").arg("").arg(""));
    series->setLabelsVisible(1);//设置柱状图顶部数据显示
    series->setLabelsPosition(QAbstractBarSeries::LabelsOutsideEnd);//设置柱状体数据在顶部
    chart->addSeries(series);//添加数据源
    chart->createDefaultAxes();//创建默认的X Y左边轴
    chart->setAxisX(axiList, series);//设置X轴坐标
    if(axiList->categories().size()>10)
        axiList->setLabelsAngle(90);
    //chart->setMinimumWidth(3000);
    chart->setGeometry(QRect(0,0,1000,100));
    QValueAxis *axisY = new QValueAxis; //Y 轴
    axisY->setRange(0,max+5);
    axisY->setTitleText("人数");
    axisY->setLabelFormat("%d"); //标签格式
    axisY->setTickCount(5);
    axisY->setMinorTickCount(4);
    chart->setAxisY(axisY, series);
    chart->legend()->setVisible(true); //设置图例为显示状态
    chart->legend()->setAlignment(Qt::AlignTop);//设置图例的显示位置在底部
    chart->setTheme(QChart::ChartThemeBlueCerulean);//设置个帅气的背景（别的还没试）

}

QChart *HistogramManager::getChart()
{
    return chart;
}
